<?php

namespace App\Imports;

use App\Classify;
use App\Merchant;
use App\MerchantMedicine;
use App\MerchantMedicineImg;
use App\MerchantMedicineXClassify;
use App\SystemMedicine;
use App\SystemMedicineImg;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class MedicineImport implements ToCollection
{
    protected $dddd=0;
    protected $type='input';
    protected $abnormalData=[];
    protected $systemMedicineArr = [];
    protected $merchantArr = [];
    protected $classifyOneArr = [];
    protected $classifyTwoArr = [];
    protected $merchantMedicineArr = [];

    public function setAbnormalData($data){
        $this->abnormalData = $data;
    }

    public function __construct($enterprice,$type)
    {
        $this->dddd = $enterprice;
        $this->type = $type;
    }

    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        foreach ($collection as $key => $value) {
            if ($key > 1) {
                $upc = $value[0];
                $store = $value[1];
                $firstClassify = $value[2];
                $secondClassify = $value[3];
                $sellingPrice = $value[4];
                $stock = $value[5];
                $isShop = $value[6];
                $sort = $value[7];

                if (!is_numeric(trim($upc))) {
                    $upc = preg_replace('/[^0-9 ]/i', '', $upc);
                }

                $systemMedicine = SystemMedicine::whereUpc($upc)->first();
                $merchant = Merchant::whereEnterpriseId($this->dddd)->whereName($store)->first();
                if ($merchant) {
                    $classifyOne = Classify::whereEnterpriseId($this->dddd)->whereMerchantId($merchant->id)->whereType('selling')->whereName($firstClassify)->first();
                    if ($classifyOne) {
                        $classifyTwo = Classify::whereEnterpriseId($this->dddd)->whereMerchantId($merchant->id)->wherePid($classifyOne->id)->whereType('selling')->whereName($secondClassify)->first();
                    } else {
                        $classifyTwo = null;
                    }
                } else {
                    $classifyOne = null;
                    $classifyTwo = null;
                }

                if ($this->type == 'input') {
                    if ($systemMedicine && $merchant && $classifyOne && $classifyTwo) {
                        $merchantMedicine = MerchantMedicine::whereMerchantId($merchant->id)->whereUpc($upc)->first();
                        if (!$merchantMedicine) {
                            $newMerchantMedicine = new MerchantMedicine();
                            $newMerchantMedicine->upc = $upc;
                            $newMerchantMedicine->name = $systemMedicine->name;
                            $newMerchantMedicine->merchant_id = $merchant->id;
                            $newMerchantMedicine->approval_number = $systemMedicine->approval_number;
                            $newMerchantMedicine->specification = $systemMedicine->specification;
                            $newMerchantMedicine->selling_price = $sellingPrice ? $sellingPrice : 0;
                            $newMerchantMedicine->stock = $stock ? $stock : 0;
                            $newMerchantMedicine->manufacturer = $systemMedicine->manufacturer;
                            $newMerchantMedicine->manufacturer_address = $systemMedicine->manufacturer_address;
                            $newMerchantMedicine->manufacturer_contact = $systemMedicine->manufacturer_contact;
                            $newMerchantMedicine->electronic_regulatory_code = $systemMedicine->electronic_regulatory_code;
                            $newMerchantMedicine->usage_and_dosage = $systemMedicine->usage_and_dosage;
                            $newMerchantMedicine->indications = $systemMedicine->indications;
                            $newMerchantMedicine->adapt_to_the_crowd = $systemMedicine->adapt_to_the_crowd;
                            $newMerchantMedicine->component = $systemMedicine->component;
                            $newMerchantMedicine->adverse_reactions = $systemMedicine->adverse_reactions;
                            $newMerchantMedicine->contraindications = $systemMedicine->contraindications;
                            $newMerchantMedicine->precautions = $systemMedicine->precautions;
                            $newMerchantMedicine->interaction = $systemMedicine->interaction;
                            $newMerchantMedicine->medication_for_children = $systemMedicine->medication_for_children;
                            $newMerchantMedicine->medication_for_pregnancy_x_lactation = $systemMedicine->medication_for_pregnancy_x_lactation;
                            $newMerchantMedicine->medication_for_aged = $systemMedicine->medication_for_aged;
                            $newMerchantMedicine->properties = $systemMedicine->properties;
                            $newMerchantMedicine->packing = $systemMedicine->packing;
                            $newMerchantMedicine->dosage_form = $systemMedicine->dosage_form;
                            $newMerchantMedicine->storage = $systemMedicine->storage;
                            $newMerchantMedicine->pharmacological_action = $systemMedicine->pharmacological_action;
                            $newMerchantMedicine->expiry_date = $systemMedicine->expiry_date;
                            $newMerchantMedicine->detail = $systemMedicine->detail;
                            $newMerchantMedicine->is_shop = $isShop ? $isShop : 0;
                            $newMerchantMedicine->sort = $sort ? $sort : 0;
                            $newMerchantMedicine->save();

                            $newMerchantMedicineClassify = new MerchantMedicineXClassify();
                            $newMerchantMedicineClassify->merchant_medicine_id = $newMerchantMedicine->id;
                            $newMerchantMedicineClassify->classify_id = $classifyOne->id;
                            $newMerchantMedicineClassify->save();

                            $newMerchantMedicineClassifyTwo = new MerchantMedicineXClassify();
                            $newMerchantMedicineClassifyTwo->merchant_medicine_id = $newMerchantMedicine->id;
                            $newMerchantMedicineClassifyTwo->classify_id = $classifyTwo->id;
                            $newMerchantMedicineClassifyTwo->save();

                            $systemMedicineImgList = SystemMedicineImg::whereSystemMedicineId($systemMedicine->id)->get();
                            if (count($systemMedicineImgList) > 0) {
                                foreach ($systemMedicineImgList as $systemMedicineImg) {
                                    $newMerchantMedicineImg = new MerchantMedicineImg();
                                    $newMerchantMedicineImg->merchant_medicine_id = $newMerchantMedicine->id;
                                    $newMerchantMedicineImg->img_id = $systemMedicineImg->img_id;
                                    $newMerchantMedicineImg->sort = $systemMedicineImg->sort;
                                    $newMerchantMedicineImg->save();
                                }
                            }
                        }
                    }
                } else {
                    if (!$systemMedicine || !$merchant || !$classifyOne || !$classifyTwo) {
                        $abnormalData = ['line' => $key + 1,
                            'message' => '数据异常，请核对数据',
//                            'upc' => $upc,
//                            'store' => $store,
//                            'classify_one' => $firstClassify,
//                            'classify_two' => $secondClassify,
//                            'systemMedicine' => $systemMedicine,
//                            'merchant' => $merchant,
//                            'classifyOne' => $classifyOne,
//                            'classifyTwo' => $classifyTwo,
                        ];
                        array_push($this->abnormalData, $abnormalData);
                    } else {
                        $merchantMedicine = MerchantMedicine::whereMerchantId($merchant->id)->whereUpc($upc)->first();
                        if ($merchantMedicine) {
                            $abnormalData = ['line' => $key + 2, 'message' => '数据已存在'];
                            array_push($this->abnormalData, $abnormalData);
                        }
                    }
                }
            }
        }
    }
//    public function collection(Collection $collection)
//    {
//        foreach ($collection as $key => $value) {
//            if ($key > 1) {
//                $upc = $value[0];
//                $store = $value[1];
//                $firstClassify = $value[2];
//                $secondClassify = $value[3];
//                $sellingPrice = $value[4];
//                $stock = $value[5];
//                $isShop = $value[6];
//                $sort = $value[7];
//
//                if (!is_numeric(trim($upc))) {
//                    $upc = preg_replace('/[^0-9 ]/i', '', $upc);
//                }
//
//                $upc = trim($upc);
//                $store = trim($store);
//                $firstClassify = trim($firstClassify);
//                $secondClassify = trim($secondClassify);
//
//                //平台库药品
//                $systemMedicine = SystemMedicine::whereUpc($upc)->first();
//
//                //商家店铺
//                $storeExists = isset($this->merchantArr[$store]);
//                if($storeExists){
//                    $merchantId = $this->merchantArr[$store];
//                }else{
//                    $merchant = Merchant::whereEnterpriseId($this->dddd)->whereName($store)->first();
//                    if($merchant){
//                        $merchantId = $merchant->id;
//                    }else{
//                        $merchantId = 0;
//                    }
//
//                    $this->merchantArr[$store] = $merchantId;
//                }
//
//                //一级分类
//                $firstClassifyExists = isset($this->classifyOneArr[$firstClassify]);
//                if($firstClassifyExists){
//                    $classifyOneId = $this->classifyOneArr[$firstClassify];
//                }else{
//                    $classifyOne = Classify::whereEnterpriseId($this->dddd)->whereMerchantId($merchantId)->whereType('selling')->whereName($firstClassify)->first();
//                    if($classifyOne){
//                        $classifyOneId = $classifyOne->id;
//                    }else{
//                        $classifyOneId = 0;
//                    }
//
//                    $this->classifyOneArr[$firstClassify] = $classifyOneId;
//                }
//
//                //二级分类
//                $secondClassifyExists = isset($this->classifyTwoArr[$secondClassify]);
//                if($secondClassifyExists){
//                    $classifyTwoId = $this->classifyTwoArr[$secondClassify];
//                }else{
//                    $classifyTwo = Classify::whereEnterpriseId($this->dddd)->whereMerchantId($merchantId)->wherePid($classifyOneId)->whereType('selling')->whereName($secondClassify)->first();
//                    if($classifyTwo){
//                        $classifyTwoId = $classifyTwo->id;
//                    }else{
//                        $classifyTwoId = 0;
//                    }
//
//                    $this->classifyTwoArr[$secondClassify] = $classifyTwoId;
//                }
//
//                //门店药品
//                $merchantUpcExists = isset($this->merchantMedicineArr[$upc]);
//                if($merchantUpcExists){
//                    $merchantMedicineId = $this->merchantMedicineArr[$upc];
//                }else{
//                    $merchantMedicine = MerchantMedicine::whereMerchantId($merchantId)->whereUpc($upc)->first();
//                    if($merchantMedicine){
//                        $merchantMedicineId = $merchantMedicine->id;
//                    }else{
//                        $merchantMedicineId = 0;
//                    }
//                    $this->merchantMedicineArr[$upc] = $merchantMedicineId;
//                }
//
//                if ($this->type == 'input') {
//                    if ($systemMedicine && $merchantId && $classifyOneId && $classifyTwoId) {
//                        if (!$merchantMedicineId) {
//                            $newMerchantMedicine = new MerchantMedicine();
//                            $newMerchantMedicine->upc = $upc;
//                            $newMerchantMedicine->name = $systemMedicine->name;
//                            $newMerchantMedicine->merchant_id = $merchantId;
//                            $newMerchantMedicine->approval_number = $systemMedicine->approval_number;
//                            $newMerchantMedicine->specification = $systemMedicine->specification;
//                            $newMerchantMedicine->selling_price = $sellingPrice ? $sellingPrice : 0;
//                            $newMerchantMedicine->stock = $stock ? $stock : 0;
//                            $newMerchantMedicine->manufacturer = $systemMedicine->manufacturer;
//                            $newMerchantMedicine->manufacturer_address = $systemMedicine->manufacturer_address;
//                            $newMerchantMedicine->manufacturer_contact = $systemMedicine->manufacturer_contact;
//                            $newMerchantMedicine->electronic_regulatory_code = $systemMedicine->electronic_regulatory_code;
//                            $newMerchantMedicine->usage_and_dosage = $systemMedicine->usage_and_dosage;
//                            $newMerchantMedicine->indications = $systemMedicine->indications;
//                            $newMerchantMedicine->adapt_to_the_crowd = $systemMedicine->adapt_to_the_crowd;
//                            $newMerchantMedicine->component = $systemMedicine->component;
//                            $newMerchantMedicine->adverse_reactions = $systemMedicine->adverse_reactions;
//                            $newMerchantMedicine->contraindications = $systemMedicine->contraindications;
//                            $newMerchantMedicine->precautions = $systemMedicine->precautions;
//                            $newMerchantMedicine->interaction = $systemMedicine->interaction;
//                            $newMerchantMedicine->medication_for_children = $systemMedicine->medication_for_children;
//                            $newMerchantMedicine->medication_for_pregnancy_x_lactation = $systemMedicine->medication_for_pregnancy_x_lactation;
//                            $newMerchantMedicine->medication_for_aged = $systemMedicine->medication_for_aged;
//                            $newMerchantMedicine->properties = $systemMedicine->properties;
//                            $newMerchantMedicine->packing = $systemMedicine->packing;
//                            $newMerchantMedicine->dosage_form = $systemMedicine->dosage_form;
//                            $newMerchantMedicine->storage = $systemMedicine->storage;
//                            $newMerchantMedicine->pharmacological_action = $systemMedicine->pharmacological_action;
//                            $newMerchantMedicine->expiry_date = $systemMedicine->expiry_date;
//                            $newMerchantMedicine->detail = $systemMedicine->detail;
//                            $newMerchantMedicine->is_shop = $isShop ? $isShop : 0;
//                            $newMerchantMedicine->sort = $sort ? $sort : 0;
//                            $newMerchantMedicine->save();
//
//                            $newMerchantMedicineClassify = new MerchantMedicineXClassify();
//                            $newMerchantMedicineClassify->merchant_medicine_id = $newMerchantMedicine->id;
//                            $newMerchantMedicineClassify->classify_id = $classifyOneId;
//                            $newMerchantMedicineClassify->save();
//
//                            $newMerchantMedicineClassifyTwo = new MerchantMedicineXClassify();
//                            $newMerchantMedicineClassifyTwo->merchant_medicine_id = $newMerchantMedicine->id;
//                            $newMerchantMedicineClassifyTwo->classify_id = $classifyTwoId;
//                            $newMerchantMedicineClassifyTwo->save();
//
//                            $systemMedicineImgList = SystemMedicineImg::whereSystemMedicineId($systemMedicine->id)->get();
//                            if (count($systemMedicineImgList) > 0) {
//                                foreach ($systemMedicineImgList as $systemMedicineImg) {
//                                    $newMerchantMedicineImg = new MerchantMedicineImg();
//                                    $newMerchantMedicineImg->merchant_medicine_id = $newMerchantMedicine->id;
//                                    $newMerchantMedicineImg->img_id = $systemMedicineImg->img_id;
//                                    $newMerchantMedicineImg->sort = $systemMedicineImg->sort;
//                                    $newMerchantMedicineImg->save();
//                                }
//                            }
//                        }
//                    }
//                } else {
//                    if (!$systemMedicine || !$merchantId || !$classifyOneId || !$classifyTwoId) {
//                        $abnormalData = ['line' => $key + 1,
//                            'message' => '数据异常，请核对数据',
////                            'upc' => $upc,
////                            'store' => $store,
////                            'classify_one' => $firstClassify,
////                            'classify_two' => $secondClassify,
////                            'systemMedicine' => $systemMedicine,
////                            'merchant' => $merchant,
////                            'classifyOne' => $classifyOne,
////                            'classifyTwo' => $classifyTwo,
//                        ];
//                        array_push($this->abnormalData, $abnormalData);
//                    } else {
//                        if ($merchantMedicineId) {
//                            $abnormalData = ['line' => $key + 2, 'message' => '数据已存在'];
//                            array_push($this->abnormalData, $abnormalData);
//                        }
//                    }
//                }
//            }
//        }
//    }

    public function getbnormalData(){
        return $this->abnormalData;
    }
}
